package som.simon.reactor;

import reactor.core.publisher.Flux;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

public class GenerateMain {

    public static void main(String[] args) throws IOException {

        // simpleGenerateUse();
        
        Flux<String> flux = Flux.generate(
                AtomicLong::new,
                (state, sink) -> {
                    long i = state.getAndIncrement();
                    sink.next("3 x " + i + " = " + 3*i);
                    if (i == 10) sink.complete();
                    return state;
                });

        flux.subscribe(System.out::println);

        System.in.read();

    }

    private static void simpleGenerateUse() {
        Flux<String> flux = Flux.generate(
                () -> 0,
                (state, sink) -> {
                    sink.next("3 x " + state + " = " + 3*state);
                    if (state == 10) sink.complete();
                    return state + 1;
                });


        flux.subscribe(System.out::println);
    }

}
